#pragma once
#include "Thread.hpp"
#include "Log.hpp"
#include "Task.hpp"
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <string>
#include <vector>
#include <queue>

using namespace std;
using namespace yss;

template <class T>
class ThreadPool
{
private:
    void Lock()
    {
        pthread_mutex_lock(&_mutex);
    }
    void UnLock()
    {
        pthread_mutex_unlock(&_mutex);
    }
    void ThreadSleep()
    {
        pthread_cond_wait(&_cond, &_mutex);
    }
    void WakeOne()
    {
        pthread_cond_signal(&_cond);
    }
    void WakeAll()
    {
        pthread_cond_broadcast(&_cond);
    }
public:
    ThreadPool(int num):_threadnum(num),_wait_num(0),_isrunning(false)
    {
        pthread_mutex_init(&_mutex, nullptr);
        pthread_cond_init(&_cond, nullptr);
        LOG(INFO, "ThreadPool Construct()");
    }
    void HandlerTask(string &name)
    {
        LOG(INFO, "%s is running...", name.c_str());
        while(true)
        {
            Lock();
            if(_task_queue.empty() && _isrunning)
            {
                _wait_num++;
                ThreadSleep();
                _wait_num--;
            }
            if(_task_queue.empty() && !_isrunning)
            {
                UnLock();
                //cout << name << " quit..." << endl;
                sleep(1);
                break;
            }
            //
            T t = _task_queue.front();
            _task_queue.pop();

            UnLock();

            LOG(DEBUG, "%s get a task", name.c_str());
            // 处理任务
            t();
            LOG(DEBUG, "%s handler a task, result is: %s", name.c_str(), t.RTostring().c_str());

        }
    }
    void InitThreadPool()
    {
        for(int i=0; i<_threadnum; i++)
        {
            string name = "thread-" + to_string(i+1);
            _threads.emplace_back(bind(&ThreadPool::HandlerTask, this, placeholders::_1), name);
            LOG(INFO, "init thread %s done", name.c_str());
        }
        _isrunning = true;
    }

    void Start()
    {
        for(auto &t:_threads)
        {
            t.Start();
        }
    }

    void Stop()
    {
        Lock();
        _isrunning = false;
        WakeAll();
        UnLock();
    }

    bool Enqueue(T &in)
    {
        Lock();
        bool ret = false;
        if(_isrunning)
        {
            _task_queue.push(in);
            if(_wait_num > 0)
            {
                WakeOne();
            }
            LOG(DEBUG, "enqueue task success");
            ret = true;
        }
        UnLock();
        return ret;
    }

    void Wait()
    {
        for(auto &t:_threads)
        {
            t.Join();
            LOG(INFO, "%s is quit...", t.Name().c_str());
        }
    }

    ~ThreadPool()
    {
        pthread_mutex_destroy(&_mutex);
        pthread_cond_destroy(&_cond);
    }

private:
    int _threadnum;
    vector<Thread> _threads;
    queue<T> _task_queue;
    pthread_mutex_t _mutex;
    pthread_cond_t _cond;
    int _wait_num;
    bool _isrunning;
};